﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OT.Data;
using OT.Areas.Admin.Models;
using PagedList;

namespace OT.Areas.Admin.Controllers
{
    public class CountryController : Controller
    {
        #region Declaration
        int LanguageID = Utilities.GetLanguageID();
        readonly OTEntitiesme OTDb = new OTEntitiesme();
        #endregion

        #region Index
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public JsonResult Index(int pageNumber, int pageSize, string searchString, int pgindex)
        {
            List<Countries> Country = (from c in OTDb.Countries
                                select new Countries
                                {
                                    CountryID = c.CountryID,
                                    CountryCode = c.CountryCode,
                                    CountryName = c.CountryName,
                                    CreatedDate = (c.CreatedDate.HasValue) ? c.CreatedDate.Value : DateTime.MinValue,
                                    UpdatedDate = (c.UpdatedDate.HasValue) ? c.UpdatedDate.Value : DateTime.MinValue
                                }).OrderBy(o => o.CountryName).ToList<Countries>();

            if (!string.IsNullOrEmpty(searchString))
                Country = Country.Where(r => r.CountryName.ToLower().Contains(searchString.ToLower())).ToList();
            int recount = Country.Count;
            Country = Country.Skip<Countries>(pgindex * (pageNumber - 1))
                .Take<Countries>(pgindex).ToList<Countries>();

            return Json(new
            {
                PageNumber = pageNumber,
                PageSize = pageSize,
                PgIndex = pgindex,
                Data = Country,
                RecordCount = recount
            });
        }
        #endregion

        #region Create
        public ActionResult Create()
        {
            return PartialView();
        }

        [HttpPost]
        public string Create(string CountryCode, string CountryName)
        {
            string result = "";
            Country Country = null;
            try
            {
                if (ModelState.IsValid)
                {
                    Country = new Country();
                    Country.CountryCode = CountryCode;
                    Country.CountryName = CountryName;
                    Country.CreatedDate = DateTime.Now;
                    Country.UpdatedDate = DateTime.Now;
                    OTDb.Countries.Add(Country);
                    OTDb.SaveChanges();
                    result = "Sucess";
                }
                else
                {
                    var Citys = OTDb.States.ToList();
                    result = "NotValid";
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return result;
        }
        #endregion

        #region Edit
        public ActionResult Edit(int CountryID)
        {
            Countries Country = null;
            try
            {
                var categry = OTDb.Countries.Where(r => r.CountryID == CountryID).SingleOrDefault();
                if (categry != null)
                {
                    Country = new Countries();
                    Country.CountryID = categry.CountryID;
                    Country.CountryName = categry.CountryName;
                    Country.CountryCode = categry.CountryCode;
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return PartialView(Country);
        }
        [HttpPost]
        public string Edit(int CountryID, string CountryCode, string CountryName)
        {
            string result = "";
            try
            {
                if (ModelState.IsValid)
                {
                    var County = OTDb.Countries.Where(r => r.CountryID == CountryID).SingleOrDefault();
                    if (County != null)
                    {
                        County.CountryName = CountryName;
                        County.CountryCode = CountryCode;
                        County.UpdatedDate = DateTime.Now;
                        OTDb.SaveChanges();
                        result = "Sucess";
                    }
                }
                else
                {
                    var category = OTDb.Countries.ToList();
                    result = "NotValid";
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return result;
        }
        #endregion

        #region Delete
        public ActionResult Delete(int CountryID)
        {
           Countries country = null;
            try
            {
                var categry = OTDb.Countries.Where(r => r.CountryID == CountryID).SingleOrDefault();
                if (categry != null)
                {
                    country = new Countries();
                    country.CountryID = categry.CountryID;
                    country.CountryName = categry.CountryName;
                    country.CountryCode = categry.CountryCode;
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return PartialView(country);
        }

        [HttpPost]
        public string Delete(int CountryID, string IsDelete)
        {
            string result = "";
            try
            {
                var Country = OTDb.Countries.Where(r => r.CountryID == CountryID).SingleOrDefault();
                if (Country != null)
                {
                    OTDb.Countries.Remove(Country);
                    OTDb.SaveChanges();
                    result = "Success";
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return result;
        }
        #endregion
    }
}
